package org.zch.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.zch.common.ApiResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.zch.entity.dto.OrdersDTO;
import org.zch.entity.po.OrdersPO;
import org.zch.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单表(Order)表控制层
 *
 * @author 刘新
 * @since 2024-01-10 11:19:40
 */
@RestController
@RequestMapping("orders")
@Api(tags = "订单")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;

    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @param ordersPO 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询所有数据", notes = "分页查询所有数据")
    @GetMapping("/getPage")
    public ApiResult selectAll(Page<OrdersPO> page, OrdersPO ordersPO) {
        return ApiResult.ok(this.ordersService.page(page, new QueryWrapper<>(ordersPO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据")
    @GetMapping("/getById/{id}")
    public ApiResult selectOne(@PathVariable Integer id) {
        return ApiResult.ok(this.ordersService.getOrderById(id));
    }

    /**
     * 生成订单
     *
     * @param ordersPO 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody OrdersPO ordersPO) {
        return ApiResult.ok(this.ordersService.insert(ordersPO));
    }

    /**
     * 修改数据
     *
     * @param ordersPO 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @PostMapping("/update")
    public ApiResult update(@RequestBody OrdersPO ordersPO) {
        return ApiResult.ok(this.ordersService.updateById(ordersPO));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @PostMapping("/deleteByIds")
    public ApiResult delete(@RequestBody List<Integer> idList) {
        return ApiResult.ok(this.ordersService.removeByIds(idList));
    }


    /**
     * 查询订单列表
     * @param dto 查询条件
     * @return 订单分页列表
     */
    @ApiOperation(value = "通过用户code查询订单列表", notes = "查询订单列表")
    @PostMapping("/getOrders")
    public ApiResult getOrders(@RequestBody OrdersDTO dto) {
        return ApiResult.ok(this.ordersService.getOrders(dto));
    }

}

